package zozopo.operations.business;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class IPReportOS extends OperatingService{
	public IPReportOS(Connection conn){
		super(conn);
	}
	
	public IPReportOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @see releaseConnection()
	 */
	public void releaseConnection(){
		databaseConnection.releaseConnection(conn);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insertReport(IPReportTB ipreport){
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		insertMap.put(IPReportTB.NAME_IPREPORT, ipreport.getName_ipreport());
		insertMap.put(IPReportTB.IP_IPREPORT, ipreport.getIp_ipreport());
		insertMap.put(IPReportTB.URL_IPREPORT, ipreport.getUrl_ipreport());
		
		return sqlMachine.insertRow(IPReportTB.TABLE_NAME, insertMap);
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<IPReportTB> selectIPReportBySql(String sql){
		List<IPReportTB> ipReportList = null;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					ipReportList = new ArrayList<IPReportTB>();
					while(resultSet.next()){
						IPReportTB ipRport = new IPReportTB();
						
						ipRport.setName_ipreport(resultSet.getString(IPReportTB.NAME_IPREPORT));
						ipRport.setUrl_ipreport(resultSet.getString(IPReportTB.URL_IPREPORT));
						ipRport.setIp_ipreport(resultSet.getString(IPReportTB.IP_IPREPORT));
						ipRport.setDate_ipreport(resultSet.getString(IPReportTB.DATE_IPREPORT));
						
						ipReportList.add(ipRport);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return ipReportList;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<IPReportTB> selectIPReport(){
		
		List<IPReportTB> ipReportList = null;
		String sql = "SELECT * FROM " + IPReportTB.TABLE_NAME + " order by " + IPReportTB.DATE_IPREPORT + " desc";
		ipReportList = selectIPReportBySql(sql);
		
		return ipReportList;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<IPReportTB> selectIPReport(int beginPageNum,int pageSize){
		
		List<IPReportTB> ipReportList = null;
		String sql = "SELECT * FROM " + IPReportTB.TABLE_NAME + " order by " + IPReportTB.DATE_IPREPORT + " desc limit "+beginPageNum+","+pageSize; 
		ipReportList = selectIPReportBySql(sql);
		
		return ipReportList;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(int beginPageNum,int pageSize){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + IPReportTB.TABLE_NAME;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean deleteAllRecord(){
		SqlMachine sqlMachine = new SqlMachine(conn);
		return sqlMachine.deleteAllRow(IPReportTB.TABLE_NAME);
	}
	
	
}
